#include "analogclockwidget.h"

AnalogClockWidget::AnalogClockWidget(QWidget* parent)
	: QWidget(parent)
	, m_showSeconds(true)
	, m_darkTheme(false)
{
	setMinimumSize(200, 200);
}

void AnalogClockWidget::setTime(const QDateTime& time)
{
	m_time = time;
	update();
}

void AnalogClockWidget::setShowSeconds(bool show)
{
	m_showSeconds = show;
	update();
}

void AnalogClockWidget::setDarkTheme(bool dark)
{
	m_darkTheme = dark;
	update();
}

void AnalogClockWidget::paintEvent(QPaintEvent* event)
{
	Q_UNUSED(event);

	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);

	drawClockFace(painter);
	drawClockNumbers(painter);
	drawClockHands(painter);
}

void AnalogClockWidget::resizeEvent(QResizeEvent* event)
{
	int side = qMin(width(), height());
	m_clockRect = QRect((width() - side) / 2, (height() - side) / 2, side, side);
	QWidget::resizeEvent(event);
}

void AnalogClockWidget::drawClockFace(QPainter& painter)
{
	painter.save();

	QPoint center = m_clockRect.center();
	int radius = m_clockRect.width() / 2 - 10;

	QColor faceColor = m_darkTheme ? QColor(60, 60, 60) : Qt::white;
	QColor borderColor = m_darkTheme ? QColor(120, 120, 120) : Qt::black;

	painter.setBrush(faceColor);
	painter.setPen(QPen(borderColor, 3));
	painter.drawEllipse(center, radius, radius);

	painter.setPen(QPen(borderColor, 2));
	painter.translate(center);
	for (int i = 0; i < 60; ++i)
	{
		int length = (i % 5) ? 8 : 15;
		int width = (i % 5) ? 1 : 3;

		painter.rotate(6);
		painter.setPen(QPen(borderColor, width));
		painter.drawLine(0, -radius + 5, 0, -radius + 5 + length);
	}

	painter.restore();
}

void AnalogClockWidget::drawClockHands(QPainter& painter)
{
	if (!m_time.isValid())
		return;
	painter.save();
	
	QPoint center = m_clockRect.center();
	QTime time = m_time.time();
	qreal zoom = m_clockRect.width() / 300.0;
	int hour = time.hour() % 12;
	int minute = time.minute();
	int second = time.second();

	double hourAngle = hour * 30 + (minute >> 1) - 90; 
	double minuteAngle = minute * 6 + (second / 10.0) - 90;
	double secondAngle = second * 6 - 90;

	QColor handColor = m_darkTheme ? Qt::white : Qt::black;

	painter.translate(center);

	painter.save();
	painter.rotate(hourAngle);
	painter.setPen(QPen(handColor, 6, Qt::SolidLine, Qt::RoundCap));
	painter.drawLine(0, 0, 0, -50 * zoom);
	painter.restore();

	painter.save();
	painter.rotate(minuteAngle);
	painter.setPen(QPen(handColor, 4, Qt::SolidLine, Qt::RoundCap));
	painter.drawLine(0, 0, 0, -70 * zoom);
	painter.restore();

	if (m_showSeconds)
	{
		painter.save();
		painter.rotate(secondAngle);
		painter.setPen(QPen(Qt::red, 2, Qt::SolidLine, Qt::RoundCap));
		painter.drawLine(0, 0, 0, -80 * zoom);
		painter.restore();
	}

	painter.setBrush(handColor);
	painter.setPen(QPen(handColor, 2));
	painter.drawEllipse(-5, -5, 10, 10);

	painter.restore();
}

void AnalogClockWidget::drawClockNumbers(QPainter& painter)
{
	painter.save();

	QPoint center = m_clockRect.center();
	int radius = m_clockRect.width() / 2 - 30;

	QColor textColor = m_darkTheme ? Qt::white : Qt::black;
	painter.setPen(textColor);
	painter.setFont(QFont("Times New Roman", 14, QFont::Bold));

	for (int i = 1; i <= 12; ++i)
	{
		double angle = (i * 30 - 90) * M_PI / 180.0; 
		int x = center.x() + static_cast<int>(radius * cos(angle));
		int y = center.y() + static_cast<int>(radius * sin(angle));
		painter.drawText(QRect(x - 15, y - 15, 30, 30), Qt::AlignCenter, QString::number(i));
	}

	painter.restore();
}
